package com.farm.service.impl;

import com.farm.context.BaseContext;
import com.farm.dto.ShoppingCartDTO;
import com.farm.entity.Gift;
import com.farm.entity.Goods;
import com.farm.entity.ShoppingCart;
import com.farm.mapper.GiftMapper;
import com.farm.mapper.GoodsMapper;
import com.farm.mapper.ShoppingCartMapper;
import com.farm.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @FileName ShoppingCartServiceImpl
 * @Description
 * @Author Sword
 * @date 2025-02-27
 **/

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GiftMapper giftMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        //判断当前加入购物车中的商品是否已经存在了
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);

        if (shoppingCarts != null && shoppingCarts.size() > 0) {
            //如果已经存在了,只需要将数量加一
            ShoppingCart cart = shoppingCarts.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart);
        } else {
            //如果不存在,需要插入一条购物车数据
            //判断本次添加到购物车的是菜品还是套餐
            Long goodsId = shoppingCart.getDishId();
            if (goodsId != null) {
                //本次添加到购物车的是菜品
                Goods goods = goodsMapper.getById(goodsId);
                shoppingCart.setName(goods.getName());
                shoppingCart.setAmount(goods.getPrice());
                shoppingCart.setImage(goods.getImage());
            } else {
                //本次添加到购物车的是套餐
                Long giftId = shoppingCart.getSetmealId();
                Gift gift = giftMapper.getById(giftId);
                shoppingCart.setName(gift.getName());
                shoppingCart.setAmount(gift.getPrice());
                shoppingCart.setImage(gift.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        //获取当前微信用户的id
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;

    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }

    /**
     * 删除购物车中一个商品
     *
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        //查询当前商品的个数
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);

        //判断当前商品是否存在
        if (shoppingCarts != null && shoppingCarts.size() > 0) {
            ShoppingCart cart = shoppingCarts.get(0);
            Integer number = cart.getNumber();
            //如果存在并且数量大于1 数量减一
            if (number > 1) {
                cart.setNumber(number - 1);
                shoppingCartMapper.updateNumberById(cart);
            } else {
                //如果数量等于1 删除购物车商品
                shoppingCartMapper.delete(cart);
            }

        }

    }
}
